//
//  LKMySubordinateDetailViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/1.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKMySubordinateDetailViewController.h"
#import "LKMySubordinateHeaderIntroView.h"
#import "LKMyShopDetailDateView.h"
#import "LKMyShopDetailPriceCell.h"
#import "LKMyShopDetailTableHeaderView.h"
#import "LKMySubordinateViewController.h"
#import "LKMySuboraListDetailModel.h"
#import "LKDirectShopViewController.h"
@interface LKMySubordinateDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)LKMyShopDetailDateView *dateView;
@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)LKMySuboraListDetailModel *detailModel;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)LKMySubordinateHeaderIntroView *headerView;
@end

@implementation LKMySubordinateDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"代理详情";
    [self createShopHeaderView];
    [self getMyAgentInformationData];
//    [self getMyAgentLevelInformationData];
    // Do any additional setup after loading the view.
}
-(void)createShopHeaderView{
    self.headerView = [[[NSBundle mainBundle] loadNibNamed:@"LKMySubordinateHeaderIntroView" owner:self options:nil] lastObject];
    self.headerView.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, WIDTH, 91);
    [self.view addSubview:_headerView];
    self.dateView = [[[NSBundle mainBundle] loadNibNamed:@"LKMyShopDetailDateView" owner:self options:nil] lastObject];
    [self.view addSubview:_dateView];
    [_dateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(_headerView.mas_bottom).offset(10);
        make.height.mas_equalTo(62);
    }];
    _dateView.dateView.hidden = YES;
    [_dateView.middleView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dateView).offset(0);
        make.top.bottom.equalTo(self.dateView);
        make.width.mas_equalTo(WIDTH/2);
    }];
    [_dateView.rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.dateView).offset(0);
        make.top.bottom.equalTo(self.dateView);
        make.width.mas_equalTo(WIDTH/2);
    }];
    self.tableView = [LKBaseTableview setTableviewWithFrame:CGRectZero style:UITableViewStyleGrouped registercellClasss:@[@"LKMyShopDetailPriceCell"]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(_dateView.mas_bottom).offset(10);
        make.bottom.equalTo(self.view).offset(0);
    }];
    [self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count + 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == self.dataArray.count) {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"下级代理";
        cell.textLabel.textColor = COLOR_HEX(0x333333);
        cell.textLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    if (indexPath.section == self.dataArray.count + 1) {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"直属商户";
        cell.textLabel.textColor = COLOR_HEX(0x333333);
        cell.textLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    LKMyShopDetailPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKMyShopDetailPriceCell"];
    cell.isAgent = YES;
    if (self.dataArray.count != 0) {
        NSDictionary *valuesDic = self.dataArray[indexPath.section];
        cell.payStyle = [valuesDic allKeys][0];
        cell.rates = [NSString stringWithFormat:@"%@%%",[valuesDic allValues][0]];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 52.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LKMyShopDetailTableHeaderView *header = [[[NSBundle mainBundle] loadNibNamed:@"LKMyShopDetailTableHeaderView" owner:self options:nil] lastObject];
    header.backgroundColor = COLOR_HEX(0xFDF8F5);
    header.isAgent = YES;
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 52.f;
    }
    return 0.000000001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == self.dataArray.count - 1) {
        return  20.0f;
    }
    return 1.0f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == self.dataArray.count) {
        LKMySubordinateViewController *subordinateVc = [[LKMySubordinateViewController alloc] init];
        subordinateVc.uid = self.detailModel.uid;
        [self.navigationController pushViewController:subordinateVc animated:YES];
    }
    if (indexPath.section == self.dataArray.count + 1) {
        LKDirectShopViewController *shopVc = [[LKDirectShopViewController alloc] init];
        shopVc.uid = self.detailModel.uid;
        [self.navigationController pushViewController:shopVc animated:YES];
    }
}


/// 获取代理的详情数据
-(void)getMyAgentInformationData{
    NSDictionary *param = @{@"id":self.uid,@"token":[LKUserInforTool account].token};
    [SVProgressHUD show];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodGET serverUrl:ServerlUrl apiPath:MySuboradinateListDetailUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            [SVProgressHUD dismiss];
            self.detailModel = [LKMySuboraListDetailModel modelWithDictionary:responseObject[@"data"]];
            [self.dataArray addObjectsFromArray:@[@{@"微信":self.detailModel.wx_rate},@{@"支付宝":self.detailModel.ali_rate},@{@"云闪付一千元以上(借记)":self.detailModel.ysf_rate},@{@"云闪付一千元以上(贷记)":self.detailModel.ysf_rate},@{@"云闪付一千元以下":self.detailModel.ysf1_rate}]];
            self.headerView.detailModel = self.detailModel;
            self.dateView.suboraDetailModel = self.detailModel;
            [self.tableView reloadData];

        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
        
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
